package com.gzdh.dishrecognition.ui.adapter.defaultview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.ProductConst;
import com.gzdh.dishrecognition.event.CartEvent;
import com.gzdh.dishrecognition.model.ProductCartModel;
import com.gzdh.dishrecognition.model.WeekMenuBakModel;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class DefaultCartPreAdapter extends RecyclerView.Adapter<DefaultCartPreAdapter.MyViewHolder> {


    private LayoutInflater inflater;

    private Context mContext;

    private FoodsAdapter.OnClick onClick;

    public void setOnClick(FoodsAdapter.OnClick onClick) {
        this.onClick = onClick;
    }

    public interface OnClick {
        void userOnClick(WeekMenuBakModel.Product product);
    }


//    public void setDatas(List<WeekMenuBakModel.Product> datas) {
//        this.datas = datas;
//        notifyDataSetChanged();
//    }

    public DefaultCartPreAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
    }


    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_default_cart, parent, false);
        return new MyViewHolder(itemView);
    }

    //给每一行View填充数据
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ProductCartModel productCartModel = ProductConst.getInstance().productCartModelList.get(position);
        holder.nameTv.setText(productCartModel.getName());
        holder.countTv.setText("数量:" + String.valueOf(productCartModel.getNumber()));
        BigDecimal amountPrice = new BigDecimal(productCartModel.getPrice()).multiply(new BigDecimal(productCartModel.getNumber()));
        holder.amountTv.setText("小计：" + amountPrice.setScale(2).toPlainString());

    }


    //数据源的数量
    @Override
    public int getItemCount() {
        return ProductConst.getInstance().productCartModelList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout mainLinear;
        private TextView nameTv;
        private TextView countTv;
        private TextView amountTv;


        public MyViewHolder(View itemView) {
            super(itemView);
            nameTv = (TextView) itemView.findViewById(R.id.nameTv);
            countTv = (TextView) itemView.findViewById(R.id.countTv);
            amountTv = (TextView) itemView.findViewById(R.id.amountTv);
            mainLinear = (LinearLayout) itemView.findViewById(R.id.mainLinear);

        }
    }
}

